package com.asia5b.a5mall.middleware.logic.viewmodel.shop

import android.app.Activity
import androidx.lifecycle.MutableLiveData
import com.asia5b.a5bmall.resource.ResString
import com.asia5b.a5mall.middleware.R
import com.asia5b.a5mall.middleware.config.UserRepository
import com.asia5b.a5mall.middleware.model.shop.AbsShopModel
import com.asia5b.a5mall.middleware.model.shop.ShopCollectModel
import com.asia5b.a5mall.middleware.model.shop.ShopGoodsInfoModel
import com.asia5b.a5mall.middleware.utils.AppTool
import com.asia5b.a5mall.middleware.utils.judge
import com.asia5b.a5mall.middleware.utils.showMessage
import com.asia5b.a5mall.middleware.utils.weak
import com.asia5b.a5bmall.umeng.logic.UmengShare
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import io.reactivex.Observable
import io.reactivex.functions.Consumer
import io.reactivex.functions.Function3

/**
 * Created by zhangyuncai on 2020/1/15.
 */
class ShopActivityVM:SimpleViewModel() {

    fun getShopDetail(shopId: Int) {
        val shopDetailObserveble = shopApi.getShop(shopId)
        val shopCollectObserveble = (UserRepository.isLogin()).judge(
                shopApi.getIsCollectionShop(shopId),
                Observable.just(ShopCollectModel())
        )
        val shopGoodsNumObserveble = goodsApi.getGoodsNumForShop(shopId)
                .map {
                    return@map it
                }
        val observable = Observable.zip(
                shopDetailObserveble,
                shopCollectObserveble,
                shopGoodsNumObserveble,
                Function3<AbsShopModel, ShopCollectModel, ShopGoodsInfoModel, Array<Any>> { t1, t2, t3 ->
                    return@Function3 arrayOf(t1, t2, t3)
                })
        baseSubscribe(observable, Consumer { data ->
//            providerView().getShopDetailResult(data)
            getShopDetailResultLiveData.value=data
        })
    }

    val getShopDetailResultLiveData= MutableLiveData<Array<Any>>()
    val isCollectShopResultLiveData= MutableLiveData<ShopCollectModel>()
    val collectShopResultLiveData= MutableLiveData<Boolean>()

    fun isCollectShop(shopId: Int) {
        val shopCollectObserveble=   shopApi.getIsCollectionShop(shopId)
        baseSubscribe(shopCollectObserveble, Consumer { data ->
//            providerView().isCollectShopResult(data)
            isCollectShopResultLiveData.value=data
        })
    }


    /**
     * @Note   关注店铺
     * @param  shopId 店铺ID
     * @param  isCollect 是否关注  true/已关注
     */
    fun collectShop(shopId: Int, isCollect: Boolean) {
        if (!isCollect) {
            //https://apimall.asiajy.com/buyer/members/collection/shop
            baseSubscribe(memberApi.addCollectionShop(shopId)
                    , Consumer { data ->
                showMessage(ResString.get(R.string.关注成功))
//                providerView().collectShopResult(isCollect)
                collectShopResultLiveData.value=isCollect
            })
        } else {
            //https://apimall.asiajy.com/buyer/members/collection/shop/83
            //https://apimall.asiajy.com/buyer/members/collection/shop/83
            baseSubscribe(memberApi.deleteCollectionShop(shopId)
                    , Consumer { data ->
                showMessage(ResString.get(R.string.取消关注成功))
//                providerView().collectShopResult(isCollect)
                collectShopResultLiveData.value=isCollect
            })
        }
    }


    fun share(activity: Activity, url: String, image: String, title: String, description: String) {
        baseSubscribe(AppTool.Image.urlToBitmap(image), SimpleNetParams(isShowLoading = false), Consumer { image ->
            UmengShare.Init(activity.weak().get())
                    .web(url)
                    .setWebTitle(title)
                    .setWebImage(image)
                    .setWebDescription(description)
                    .webShare()
        }, Consumer {
            showMessage(ResString.get(R.string.分享失败))
        })
    }
}